package top.marken.onlineclass.util;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

@Component
public class BaseCache {
    /**
     * 能存储10分钟的缓存
     */
    private Cache<String, Object> tenMinuteCache;
    /**
     * 能存储1小时的缓存
     */
    private Cache<String, Object> oneHourCache;
    {
        tenMinuteCache = CacheBuilder.newBuilder()
                // 设置初始化大小
                .initialCapacity(10)
                // 设置最大数量
                .maximumSize(100)
                // 设置并发数
                .concurrencyLevel(5)
                // 设置写入后多久到期
                .expireAfterWrite(600, TimeUnit.SECONDS)
                // 统计缓存名中率
                .recordStats()
                .build();

        oneHourCache = CacheBuilder.newBuilder()
                // 设置初始化大小
                .initialCapacity(30)
                // 设置最大数量
                .maximumSize(100)
                // 设置并发数
                .concurrencyLevel(5)
                // 设置写入后多久到期
                .expireAfterWrite(1, TimeUnit.HOURS)
                // 统计缓存名中率
                .recordStats()
                .build();
    }

    public Cache<String, Object> getTenMinuteCache() {
        return tenMinuteCache;
    }

    public Cache<String, Object> getOneHourCache() {
        return oneHourCache;
    }
}
